package me.thinkjet.sso.server.ticket;

/**
 * 
 * @ClassName AbstractTicket
 * @author johnny_zyc
 * @Modified 2013-3-2 下午2:48:44
 * 
 */
public abstract class AbstractTicket implements Ticket, TicketState {

	private static final long serialVersionUID = 5201979362279469779L;

	private String id;

	private TicketGrantingTicket ticketGrantingTicket;

	private long lastTimeUsed;

	private long previousLastTimeUsed;

	private long creationTime;

	private int countOfUses;

	private Boolean expired = false;

	protected AbstractTicket() {
	}

	public AbstractTicket(final String id, final TicketGrantingTicket ticket) {
		this.id = id;
		this.creationTime = System.currentTimeMillis();
		this.lastTimeUsed = System.currentTimeMillis();
		this.ticketGrantingTicket = ticket;
	}

	public final String getId() {
		return this.id;
	}

	public final void updateState() {
		this.previousLastTimeUsed = this.lastTimeUsed;
		this.lastTimeUsed = System.currentTimeMillis();
		this.countOfUses++;
	}

	public final int getCountOfUses() {
		return this.countOfUses;
	}

	public final long getCreationTime() {
		return this.creationTime;
	}

	public final TicketGrantingTicket getGrantingTicket() {
		return this.ticketGrantingTicket;
	}

	public final long getLastTimeUsed() {
		return this.lastTimeUsed;
	}

	public final long getPreviousTimeUsed() {
		return this.previousLastTimeUsed;
	}

	public final int hashCode() {
		return 34 ^ this.getId().hashCode();
	}

	public final String toString() {
		return this.id;
	}

	public synchronized void expire() {
		this.expired = true;
	}

	public Boolean isExpiredInternal() {
		return expired;
	}

}
